/**
 * 多线程问题
 */
public class SingleTonSync {
    //加入volatile 对于volatile修饰的字段，可以防止指令重排序
    private volatile static SingleTonSync singleTonSync = null;
    //构造方法私有
    private SingleTonSync(){}

    public static SingleTonSync getSingleTonSync(){
        if (singleTonSync == null){
            //加锁操作
            synchronized (SingleTonSync.class){
                if (singleTonSync == null){
                    singleTonSync = new SingleTonSync();
                }
            }
        }

        return singleTonSync;
    }

    public static void main(String[] args) {
        new Thread( ()->{
            SingleTonSync singleTonSync = SingleTonSync.getSingleTonSync();
            System.out.println(singleTonSync);
        }).start();
        new Thread( ()->{
            SingleTonSync singleTonSync = SingleTonSync.getSingleTonSync();
            System.out.println(singleTonSync);
        }).start();

    }

}
